// pages/subcribeSort/subcribeSort.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    popIndex: 0,
    tags: [],
    allTags: [],
    chooseTags: ["", "", "", ""],
    show: false
  },

  deleteTag(e) {
    let index = e.currentTarget.dataset.index
    console.log(`删除第${index}项`)
    let temp = this.data.chooseTags;
    let tags = this.data.tags;
    console.log(tags)
    tags[index].check = new Array(tags[index].tagItem.length).fill(false)
    temp[index] = "";
    this.setData({
      chooseTags: temp,
      tags: tags
    })
  },

  //点击tag事件
  select(e) {
    let index = e.currentTarget.dataset.tag_index;
    const values = e.currentTarget.dataset.content;
    let tags = this.data.tags;
    let chooseTags = this.data.chooseTags;
    if (/[^0-9]/.test(index)) {
      let xueduan = index;
      // index = 3;
      index = 2;
      let items = tags[index].tagItem;
      for (let subIndex in items) {
        let subItem = items[subIndex];
        let check = new Array(subItem.length).fill(false);
        subItem.forEach((item, index) => {
          if (item == values && subIndex == xueduan) {
            check[index] = true;
          }
        })
        if(!tags[index]["check"]){
          tags[index]["check"] = {}
        }
        tags[index]["check"][subIndex] = check;
      }
      chooseTags[index+1] = values;
      chooseTags[index] = xueduan;
      this.setData({
        tags: tags,
        chooseTags: chooseTags,
        show:false
      })
    } else {
      let items = tags[index].tagItem;
      //重置check数组为false，再把选中的下标的元素置为true
      let check = new Array(items.length).fill(false);
      items.forEach((item, index) => {
        if (item == values) {
          check[index] = true;
        }
      })
      tags[index].check = check;
      console.log(tags[index])
      //改变chooseTags
      chooseTags[index] = values;
      this.setData({
        tags: tags,
        chooseTags: chooseTags,
        show:false
      })
    }
  },

  //保存函数
  save() {
    getApp().globalData.chooseTags[this.data.index] = this.data.chooseTags;
    const pages = getCurrentPages()
    for (let i = 0; i < pages.length; i++) {
      if (pages[i].route.indexOf('subcribe') != -1) {
        wx.navigateBack({
          delta: pages.length - 1 - i,
        })
        return false;
      }
    }
    wx.redirectTo({
      url: '../subcribe/subcribe',
    })
  },

  //弹窗函数
  popup(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      show: true,
      popIndex: index,
      allTags: [this.data.tags[index]]
    })
  },
  //关闭弹窗
  onClose() {
    this.setData({
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.$cloud.callFunction({
      name: "doSub",
      data: {
        sign: 2,
        openid: getApp().globalData.openId,
      }
    }).then(res => {
      console.log(res)
      this.setData({
        tags: res.result,
        index: options.index,
        chooseTags: getApp().globalData.chooseTags[options.index]
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})